//
//  MainWC.swift
//  swiftsandbox
//
//  Created by 杨岑 on 2018/10/9.
//  Copyright © 2018年 杨岑. All rights reserved.
//

import Cocoa

class MainWC: NSWindowController, NSToolbarDelegate {

    override func windowDidLoad() {
        super.windowDidLoad()
    
        print(Bundle.main.infoDictionary as Any)
        let title = NSLocalizedString("CFBundleName", tableName: "InfoPlist", value: "!", comment: "")
        print("App Title: \(title)")
    }
    
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return [
                NSToolbarItem.Identifier(rawValue: "test"),
                NSToolbarItem.Identifier(rawValue: "test2")
        ]
    }
    
    /*func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        
    }*/
    
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        let tbi = NSToolbarItem(itemIdentifier: itemIdentifier)
        tbi.label = itemIdentifier.rawValue
        tbi.paletteLabel = tbi.label + " in palette"
        switch itemIdentifier.rawValue {
        case "test":
            tbi.label = "test"
            tbi.paletteLabel = "Test Button"
            tbi.image = NSImage(named: NSImage.actionTemplateName)
            tbi.action = NSSelectorFromString("testButton_click:")
            tbi.target = nil
            //tbi.isEnabled = false
            tbi.autovalidates = false
            
            let menuItem = NSMenuItem(title: tbi.label, action: tbi.action, keyEquivalent: "")
            let submenu = NSMenu(title: tbi.label)
            submenu.addItem(NSMenuItem(title: "item1", action: #selector(tbiMenuItem_select), keyEquivalent: ""))
            submenu.addItem(NSMenuItem(title: "item2", action: #selector(tbiMenuItem_select), keyEquivalent: ""))
            menuItem.submenu = submenu
            tbi.menuFormRepresentation = menuItem
        default:
            break
        }
        return tbi
    }

    @IBAction func tbiMenuItem_select(_ sender: Any) {
        print("\(sender)")
    }

    @IBAction func tbiTest2_click(_ sender: Any) {
        print("\(sender)")
    }
}
